package concurrency;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class TaskScheduling {
    private static AtomicInteger count = new AtomicInteger(0);

    /*
    io密集型（线程多，等待多，单个持续时间长），计算密集型（线程少，等待少，单个任务占用时间长)
    在以上的场景当中：在1000个任务执行完成之后再来执行接下来的任务
    可选择
     */
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        int size = 10000;

        ThreadPoolExecutor computeExecutor = new ThreadPoolExecutor(32, 32, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        ThreadPoolExecutor IOExecutor = new ThreadPoolExecutor(100, 100, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());

        List<CompletableFuture<?>> list = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(count.addAndGet(1));
            }, IOExecutor);

            list.add(future);
        }
        /*
        还需要再了解的CyclicBarrier(真的是了解即可，使用场景太少了)，需要回顾一下CompletableFutrue
        */

        CompletableFuture.allOf(list.toArray(new CompletableFuture[0])).get();
        System.out.println("3");
    }
}

